package uk.co.q3c.deplan.server.service;

import uk.co.q3c.deplan.client.domain.DomainObject;
import uk.co.q3c.deplan.client.domain.dag.ChangeInvalidException;
import uk.co.q3c.deplan.client.domain.dag.CycleDetectedException;
import uk.co.q3c.deplan.client.domain.resource.PersistenceReferenceException;
import uk.co.q3c.deplan.client.domain.task.Task;
import uk.co.q3c.deplan.client.service.TaskFieldExpand;
import uk.co.q3c.deplan.server.dao.DomainDao;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.googlecode.objectify.Key;

/**
 * @see TaskService_BigT
 * @author dsowerby
 * 
 */
@SuppressWarnings("serial")
public class ServiceProviderBase extends RemoteServiceServlet {

	protected DomainDao dao;

	public ServiceProviderBase(DomainDao dao) {
		super();
		this.dao = dao;
	}

	public void setDao(DomainDao dao) {
		this.dao = dao;
	}

	public DomainDao getDao() {
		return dao;
	}

	/**
	 * Sets the domain object's discarded flag to true and saves it. If you don't want to save it automatically, set the discarded flag
	 * directly on the domain object
	 * 
	 * @param dob
	 * @throws CycleDetectedException
	 * @throws PersistenceReferenceException
	 */
	public void discard(DomainObject dob) throws CycleDetectedException, ChangeInvalidException, PersistenceReferenceException {
		dao.discard(dob);
	}

	public void closeSession() {
		dao.closeSession();
	}

	public <T extends DomainObject> T getObjectFromKey(Key<T> key) {
		return dao.getObjectFromKey(key);
	}

	public <T extends DomainObject> Key<T> keyFor(T domainObject) {
		return dao.keyFor(domainObject);
	}

	public void save(DomainObject dob) {
		dao.save(dob);
	}

	public void expand(Task task, TaskFieldExpand fieldToExpand) {
		throw new RuntimeException("Not yet implemented");
	}
}
